<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>iOS开发知识集锦 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/17.ee1bcf41.js" as="script"><link rel="prefetch" href="/assets/js/10.265e4f40.js"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/15.92adafd4.js"><link rel="prefetch" href="/assets/js/16.bcee7c14.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/20.44781dae.js"><link rel="prefetch" href="/assets/js/21.1fe46223.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/28.ce131de6.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>iOS开发知识集锦</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20170515.html#让方法只调用一次" class="sidebar-link">让方法只调用一次</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#创建单例" class="sidebar-link">创建单例</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#自定义输入视图" class="sidebar-link">自定义输入视图</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#关联对象" class="sidebar-link">关联对象</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#捕获崩溃信息" class="sidebar-link">捕获崩溃信息</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#在xcode中使用标记" class="sidebar-link">在Xcode中使用标记</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#swift实现协议的可选方法" class="sidebar-link">Swift实现协议的可选方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20170515.html#使用-objc" class="sidebar-link">使用@objc</a></li><li class="sidebar-sub-header"><a href="/Views/20170515.html#使用extension实现可选方法" class="sidebar-link">使用extension实现可选方法</a></li></ul></li><li><a href="/Views/20170515.html#自定义视图在storyboard中预览" class="sidebar-link">自定义视图在Storyboard中预览</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#masonry等间隔排列控件" class="sidebar-link">Masonry等间隔排列控件</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#优雅的限制输入框字符数量" class="sidebar-link">优雅的限制输入框字符数量</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20170515.html#去掉视图的隐式动画" class="sidebar-link">去掉视图的隐式动画</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>iOS开发知识集锦</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>iOS开发知识集锦</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2017-05-15</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20170515.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      iOS
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p>这里记录了一部分 iOS 开发中的小技巧与常见注意事项
</p> <h2 id="让方法只调用一次"><a href="#让方法只调用一次" class="header-anchor">#</a> 让方法只调用一次</h2> <p>在 Objective-C 中我们可以使用 <code>dispatch_once</code>  来让方法只调用一次，而在新的 Swift 版本中这个方法不可用，但我们可以通过 <code>lazy</code>  来做到</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token keyword">static</span> dispatch_once_t onceToken<span class="token punctuation">;</span>
<span class="token function">dispatch_once</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>onceToken<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 只执行一次的任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">lazy</span> <span class="token keyword">var</span> runOnce<span class="token punctuation">:</span> <span class="token builtin">Void</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// 这里写需要调用的方法</span>
<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">/// 在需要调用的地方这样使用</span>
<span class="token number">_</span> <span class="token operator">=</span> runOnce
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section></div></div> <h2 id="创建单例"><a href="#创建单例" class="header-anchor">#</a> 创建单例</h2> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>创建单例需要注意无论使用何种初始化方法都能正确的指向单例对象 (当然也可以简单粗暴的使用 <code>NS_UNAVAILABLE</code>  标记来禁止其他初始化方法)。
下面是一种比较安全的单例创建方法</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token keyword">static</span> SingleInstance <span class="token operator">*</span>single<span class="token punctuation">;</span>

<span class="token comment">// 正常的单例初始化入口</span>
<span class="token operator">+</span> <span class="token punctuation">(</span>instancetype<span class="token punctuation">)</span>sharedInstance<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token keyword">self</span> alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token operator">-</span> <span class="token punctuation">(</span>instancetype<span class="token punctuation">)</span>init<span class="token punctuation">{</span>
    <span class="token keyword">static</span> dispatch_once_t onceToken<span class="token punctuation">;</span>
    <span class="token function">dispatch_once</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>onceToken<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
        single <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token keyword">super</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> single<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 复写该方法确保每次创建对象都只返回同一个内存空间</span>
<span class="token operator">+</span> <span class="token punctuation">(</span>instancetype<span class="token punctuation">)</span>allocWithZone<span class="token punctuation">:</span><span class="token punctuation">(</span><span class="token keyword">struct</span> _NSZone <span class="token operator">*</span><span class="token punctuation">)</span>zone<span class="token punctuation">{</span>
    <span class="token keyword">static</span> dispatch_once_t onceToken<span class="token punctuation">;</span>
    <span class="token function">dispatch_once</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>onceToken<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
        single <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token keyword">super</span> allocWithZone<span class="token punctuation">:</span>zone<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> single<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 如果遵循了 NSCopying 协议</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span>copyWithZone<span class="token punctuation">:</span><span class="token punctuation">(</span>NSZone <span class="token operator">*</span><span class="token punctuation">)</span>zone<span class="token punctuation">{</span>
    <span class="token keyword">return</span> single<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 如果遵循了 NSMutableCopying 协议</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span>mutableCopyWithZone<span class="token punctuation">:</span><span class="token punctuation">(</span>NSZone <span class="token operator">*</span><span class="token punctuation">)</span>zone<span class="token punctuation">{</span>
    <span class="token keyword">return</span> single<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><p>这样可以确保在调用 init、new、copy、mutableCopy 时依然可以返回正确的单例。</p></section> <section aria-hidden="true" id="swift" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>一种比较简单的创建 Swift 单例的方式</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">class</span> <span class="token class-name">Single</span><span class="token punctuation">:</span> <span class="token builtin">NSObject</span> <span class="token punctuation">{</span>
    <span class="token comment">/// 单例</span>
    <span class="token keyword">static</span> <span class="token keyword">let</span> shareInstance <span class="token operator">=</span> <span class="token function">Single</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    
    <span class="token comment">/// 私有化初始化方法确保通过单例来调用</span>
    <span class="token keyword">override</span> <span class="token keyword">private</span> <span class="token keyword">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token keyword">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></section></div></div> <h2 id="自定义输入视图"><a href="#自定义输入视图" class="header-anchor">#</a> 自定义输入视图</h2> <p>我们经常需要在应用中自定义输入视图的样式，系统的 <code>UITextField</code>  往往不能满足我们的需求，尤其是密码输入方面，很多时候我们都需要自己利用 <code>UIView</code>  来绘制输入框。那如何让我们自己的输入视图也能像系统的输入框一样调起键盘输入呢？其实只要让我们的自定义视图实现 <code>UIKeyInput</code>  协议就可以了。同时需要实现下面的方法</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 记录输入的字符串</span>
<span class="token keyword">@property</span> <span class="token punctuation">(</span>nonatomic<span class="token punctuation">,</span> strong<span class="token punctuation">)</span> NSMutableString <span class="token operator">*</span>password<span class="token punctuation">;</span>

<span class="token macro property">#<span class="token directive keyword">pragma</span> mark - 协议方法实现</span>

<span class="token comment">// 返回需要开启的键盘样式</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>UIKeyboardType<span class="token punctuation">)</span>keyboardType <span class="token punctuation">{</span>
    <span class="token keyword">return</span> UIKeyboardTypeNumberPad<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 返回当前是否已经输入过文本</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>BOOL<span class="token punctuation">)</span>hasText <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">self</span><span class="token punctuation">.</span>password<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 用户输入文本的回调</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>insertText<span class="token punctuation">:</span><span class="token punctuation">(</span>NSString <span class="token operator">*</span><span class="token punctuation">)</span>text <span class="token punctuation">{</span>
    <span class="token punctuation">[</span><span class="token keyword">self</span><span class="token punctuation">.</span>password appendString<span class="token punctuation">:</span>text<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 用户输入删除的回调</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>deleteBackward <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>password<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token punctuation">[</span><span class="token keyword">self</span><span class="token punctuation">.</span>password deleteCharactersInRange<span class="token punctuation">:</span><span class="token function">NSMakeRange</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>password<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">[</span><span class="token keyword">self</span> updateDisplay<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 触摸视图时成为第一响应者来调起键盘</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>touchesBegan<span class="token punctuation">:</span><span class="token punctuation">(</span>NSSet<span class="token operator">&lt;</span>UITouch <span class="token operator">*</span><span class="token operator">&gt;</span> <span class="token operator">*</span><span class="token punctuation">)</span>touches withEvent<span class="token punctuation">:</span><span class="token punctuation">(</span>UIEvent <span class="token operator">*</span><span class="token punctuation">)</span>event<span class="token punctuation">{</span>
    <span class="token punctuation">[</span><span class="token keyword">self</span> becomeFirstResponder<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h2 id="关联对象"><a href="#关联对象" class="header-anchor">#</a> 关联对象</h2> <p>关联对象常用来给类扩展属性，使用前必须添加头文件 <code>&lt;objc/runtime.h&gt;</code> 
runtime 提供了三个方法来管理关联对象，分别对应了存储、获取、移除</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">//设置关联对象</span>
<span class="token keyword">void</span> <span class="token function">objc_setAssociatedObject</span><span class="token punctuation">(</span>id object<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">void</span> <span class="token operator">*</span>key<span class="token punctuation">,</span> id value<span class="token punctuation">,</span> objc_AssociationPolicy policy<span class="token punctuation">)</span>
<span class="token comment">//获取关联的对象</span>
id <span class="token function">objc_getAssociatedObject</span><span class="token punctuation">(</span>id object<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">void</span> <span class="token operator">*</span>key<span class="token punctuation">)</span>
<span class="token comment">//移除关联的对象</span>
<span class="token keyword">void</span> <span class="token function">objc_removeAssociatedObjects</span><span class="token punctuation">(</span>id object<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>其中的参数</p> <ul><li>id object：被关联的对象</li> <li>const void *key：关联的 key，要求唯一</li> <li>id value：关联的对象</li> <li>objc_AssociationPolicy policy：内存管理的策略</li></ul> <p>内存管理策略如下</p> <table><thead><tr><th style="text-align:center;">objc_AssociationPolicy</th> <th style="text-align:center;">modifier</th></tr></thead> <tbody><tr><td style="text-align:center;">OBJC_ASSOCIATION_ASSIGN</td> <td style="text-align:center;">assign</td></tr> <tr><td style="text-align:center;">OBJC_ASSOCIATION_RETAIN_NONATOMIC</td> <td style="text-align:center;">nonatomic, strong</td></tr> <tr><td style="text-align:center;">OBJC_ASSOCIATION_COPY_NONATOMIC</td> <td style="text-align:center;">nonatomic, copy</td></tr> <tr><td style="text-align:center;">OBJC_ASSOCIATION_RETAIN</td> <td style="text-align:center;">atomic, strong</td></tr> <tr><td style="text-align:center;">OBJC_ASSOCIATION_COPY</td> <td style="text-align:center;">atomic, copy</td></tr></tbody></table> <p>常见用法</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 使用@selector()方法来做key</span>
<span class="token comment">// @selector()的返回值SEL会被映射为C字符串</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>setAssociatedObject<span class="token punctuation">:</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span>associatedObject<span class="token punctuation">{</span>
    <span class="token function">objc_setAssociatedObject</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> <span class="token keyword">@selector</span><span class="token punctuation">(</span>associatedObject<span class="token punctuation">)</span><span class="token punctuation">,</span> associatedObject<span class="token punctuation">,</span> OBJC_ASSOCIATION_RETAIN_NONATOMIC<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//_cmd在Objective-C的方法中表示当前方法的selector，也就是 @selector(associatedObject)。</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span>associatedObject<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">objc_getAssociatedObject</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> _cmd<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 使用自定义的C字符串来做key</span>
<span class="token keyword">static</span> <span class="token keyword">char</span> <span class="token operator">*</span>AssociatedObjectKey <span class="token operator">=</span> <span class="token string">&quot;AssociatedObjectKey&quot;</span><span class="token punctuation">;</span>

<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>setAssociatedObject<span class="token punctuation">:</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span>associatedObject<span class="token punctuation">{</span>
    <span class="token function">objc_setAssociatedObject</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> AssociatedObjectKey<span class="token punctuation">,</span> associatedObject<span class="token punctuation">,</span> OBJC_ASSOCIATION_RETAIN_NONATOMIC<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token operator">-</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span>associatedObject<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">objc_getAssociatedObject</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> AssociatedObjectKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="捕获崩溃信息"><a href="#捕获崩溃信息" class="header-anchor">#</a> 捕获崩溃信息</h2> <p>要捕获程序运行中的崩溃信息只需要在 <code>didFinishLaunchingWithOptions</code>  方法中使用 <code>NSSetUncaughtExceptionHandler</code>  就可以了。不过有时候我们可能使用了多个第三方的异常处理系统，他们可能使用了同样的方法来捕获异常，这样后注册的将会覆盖掉前面注册的，导致前面注册的异常处理函数不能正常工作。这时候就需要先使用 <code>NSGetUncaughtExceptionHandler</code>  方法来保存其他的异常处理方法，然后我们处理完异常后再调用保存的方法就可以避免覆盖了。</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 记录之前的崩溃回调函数</span>
<span class="token keyword">static</span> NSUncaughtExceptionHandler <span class="token operator">*</span>otherExceptionHandler <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>

<span class="token comment">// 崩溃时的处理函数</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">ExceptionHandler</span><span class="token punctuation">(</span>NSException <span class="token operator">*</span> exception<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 异常的堆栈信息</span>
    NSArray <span class="token operator">*</span> stackArray <span class="token operator">=</span> <span class="token punctuation">[</span>exception callStackSymbols<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token comment">// 出现异常的原因</span>
    NSString <span class="token operator">*</span> reason <span class="token operator">=</span> <span class="token punctuation">[</span>exception reason<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token comment">// 异常名称</span>
    NSString <span class="token operator">*</span> name <span class="token operator">=</span> <span class="token punctuation">[</span>exception name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token comment">// 这里可以保存或上传日志</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token comment">// 调用之前保存崩溃的回调函数</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>otherExceptionHandler<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">otherExceptionHandler</span><span class="token punctuation">(</span>exception<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token operator">-</span> <span class="token punctuation">(</span>BOOL<span class="token punctuation">)</span>application<span class="token punctuation">:</span><span class="token punctuation">(</span>UIApplication <span class="token operator">*</span><span class="token punctuation">)</span>application didFinishLaunchingWithOptions<span class="token punctuation">:</span><span class="token punctuation">(</span>NSDictionary <span class="token operator">*</span><span class="token punctuation">)</span>launchOptions <span class="token punctuation">{</span>
    <span class="token comment">// 保存其他的异常处理函数</span>
    otherExceptionHandler <span class="token operator">=</span> <span class="token function">NSGetUncaughtExceptionHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 注册我们的异常处理函数</span>
    <span class="token function">NSSetUncaughtExceptionHandler</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ExceptionHandler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> YES<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h2 id="在xcode中使用标记"><a href="#在xcode中使用标记" class="header-anchor">#</a> 在 Xcode 中使用标记</h2> <p>Xcode 提供了三种实用的简易标记，即 MARK、TODO、FIXME, Xcode 将会在代码中寻找这样的注释，然后以粗体标签的形式将名称显示在导航栏。</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">//TODO: 标记将来要完成的内容</span>
<span class="token comment">//MARK: 标记一件事情</span>
<span class="token comment">//FIXME: 标记以后要修正或完善的内容</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>Xcode 还支持？？？与！！！标记</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// ???:  疑问的地方</span>
<span class="token comment">/// !!!: 需要注意的地方</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>那怎样防止忘记了代码中的这些标记呢，我们可以通过一段 shell 脚本来解决。
在 target--&gt;build phases 中新建 shell 脚本：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token assign-left variable">TAGS</span><span class="token operator">=</span><span class="token string">&quot;TODO:|FIXME:|WARNING:&quot;</span>
<span class="token assign-left variable">ERRORTAG</span><span class="token operator">=</span><span class="token string">&quot;ERROR:&quot;</span>
<span class="token function">find</span> <span class="token string">&quot;<span class="token variable">${SRCROOT}</span>&quot;</span> <span class="token punctuation">\</span><span class="token punctuation">(</span> -name <span class="token string">&quot;*.h&quot;</span> -or -name <span class="token string">&quot;*.m&quot;</span> -or -name <span class="token string">&quot;*.swift&quot;</span> <span class="token punctuation">\</span><span class="token punctuation">)</span> -print0 <span class="token operator">|</span> <span class="token function">xargs</span> -0 <span class="token function">egrep</span> --with-filename --line-number --only-matching <span class="token string">&quot;(<span class="token variable">$TAGS</span>).*\$|(<span class="token variable">$ERRORTAG</span>).*\$&quot;</span> <span class="token operator">|</span> perl -p -e <span class="token string">&quot;s/(<span class="token variable">$TAGS</span>)/ warning: \<span class="token variable">$1</span>/&quot;</span><span class="token operator">|</span> perl -p -e <span class="token string">&quot;s/(<span class="token variable">$ERRORTAG</span>)/ error: \<span class="token variable">$1</span>/&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>添加脚本后，编译代码，那些标记就会被当做 warning 显示出来。</p> <h2 id="swift实现协议的可选方法"><a href="#swift实现协议的可选方法" class="header-anchor">#</a> Swift 实现协议的可选方法</h2> <p>原生的 Swift protocol 里没有可选项，所有定义的方法都是必须实现的。如果想让某些协议方法变成可选的则有下面几种方法实现</p> <h3 id="使用-objc"><a href="#使用-objc" class="header-anchor">#</a> 使用 <code>@objc</code></h3> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token atrule">@objc</span> <span class="token keyword">protocol</span> <span class="token builtin">MyProtocol</span> <span class="token punctuation">{</span>
    <span class="token comment">// 可选</span>
    <span class="token atrule">@objc</span> <span class="token keyword">optional</span> <span class="token keyword">func</span> <span class="token function">optionalMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>当调用可选方法时使用下面的方式</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">self</span><span class="token punctuation">.</span>delegate<span class="token operator">?</span><span class="token punctuation">.</span>optionalMethod<span class="token operator">?</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果 <code>delegate</code>  或 <code>optionalMethod</code>  不存在时，该方法会返回 nil，这样可以判断方法是否调用成功</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">if</span> <span class="token keyword">self</span><span class="token punctuation">.</span>delegate<span class="token operator">?</span><span class="token punctuation">.</span>optionalMethod<span class="token operator">?</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">nil</span> <span class="token punctuation">{</span>
<span class="token comment">// 方法没有调用</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="使用extension实现可选方法"><a href="#使用extension实现可选方法" class="header-anchor">#</a> 使用 <code>extension</code>  实现可选方法</h3> <p>给自定义的协议添加 extension, 在 extension 中对可选方法进行默认实现，这样遵守协议的对象就可以不用实现可选方法.</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">protocol</span> <span class="token builtin">MyProtocol</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">optionalMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span>        <span class="token comment">// 可选</span>
<span class="token punctuation">}</span>

<span class="token keyword">extension</span> <span class="token builtin">MyProtocol</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">optionalMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 实现可选的方法</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="自定义视图在storyboard中预览"><a href="#自定义视图在storyboard中预览" class="header-anchor">#</a> 自定义视图在 Storyboard 中预览</h2> <p>我们经常会使用自定义视图来实现一些特殊的 UI 布局，如果同时还使用了 Storyboard 那如何让我们自己的视图可以在 Storyboard 中预览并可以在侧边栏修改自定义的属性呢。
只需要在我们的自定时视图前加上 <code>IB_DESIGNABLE</code> ，在自定义的属性前加上 <code>IBInspectable</code>  就可以了</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>IB_DESIGNABLE
<span class="token keyword">@interface</span> CustomView <span class="token punctuation">:</span> UIView
<span class="token keyword">@property</span> <span class="token punctuation">(</span>nonatomic<span class="token punctuation">,</span> assign<span class="token punctuation">)</span> IBInspectable CGFloat cornerRadius<span class="token punctuation">;</span>
<span class="token keyword">@property</span> <span class="token punctuation">(</span>nonatomic<span class="token punctuation">,</span> strong<span class="token punctuation">)</span> IBInspectable UIColor <span class="token operator">*</span>shadowColor<span class="token punctuation">;</span>
<span class="token keyword">@end</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="swift" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token atrule">@IBDesignable</span> <span class="token keyword">class</span> <span class="token class-name">CustomView</span><span class="token punctuation">:</span> <span class="token builtin">UIView</span> <span class="token punctuation">{</span>    
    <span class="token atrule">@IBInspectable</span> <span class="token keyword">var</span> cornerRadius<span class="token punctuation">:</span><span class="token builtin">CGFloat</span>
    <span class="token atrule">@IBInspectable</span> <span class="token keyword">var</span> shadowColor<span class="token punctuation">:</span><span class="token builtin">UIColor</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h2 id="masonry等间隔排列控件"><a href="#masonry等间隔排列控件" class="header-anchor">#</a> Masonry 等间隔排列控件</h2> <p>利用下面的两个 API 可以方便的排列多个视图</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">/**
 *  distribute with fixed spacing
 *
 *  @param axisType     横排还是竖排
 *  @param fixedSpacing 两个控件间隔(横排左右间隔 竖排上下间隔)
 *  @param leadSpacing  第一个控件与边缘的间隔
 *  @param tailSpacing  最后一个控件与边缘的间隔
 */</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>mas_distributeViewsAlongAxis<span class="token punctuation">:</span><span class="token punctuation">(</span>MASAxisType<span class="token punctuation">)</span>axisType withFixedSpacing<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>fixedSpacing leadSpacing<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>leadSpacing tailSpacing<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>tailSpacing<span class="token punctuation">;</span>

<span class="token comment">/**
 *  distribute with fixed item size
 *
 *  @param axisType        横排还是竖排
 *  @param fixedItemLength 控件的宽或高(横排设置宽度 竖排设置高度)
 *  @param leadSpacing     第一个控件与边缘的间隔
 *  @param tailSpacing     最后一个控件与边缘的间隔
 */</span>
<span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>mas_distributeViewsAlongAxis<span class="token punctuation">:</span><span class="token punctuation">(</span>MASAxisType<span class="token punctuation">)</span>axisType withFixedItemLength<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>fixedItemLength leadSpacing<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>leadSpacing tailSpacing<span class="token punctuation">:</span><span class="token punctuation">(</span>CGFloat<span class="token punctuation">)</span>tailSpacing<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>如等间隔横向排列 Label 控件</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSMutableArray <span class="token operator">*</span>gridArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSMutableArray alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span>NSInteger index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> <span class="token number">4</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    UILabel <span class="token operator">*</span>label <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>UILabel alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
    label<span class="token punctuation">.</span>textAlignment <span class="token operator">=</span> NSTextAlignmentCenter<span class="token punctuation">;</span>
    <span class="token punctuation">[</span><span class="token keyword">self</span> addSubview<span class="token punctuation">:</span>label<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">[</span>gridArr addObject<span class="token punctuation">:</span>label<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 横向排列  控件宽度50  等间隔分布</span>
<span class="token punctuation">[</span>gridArr mas_distributeViewsAlongAxis<span class="token punctuation">:</span>MASAxisTypeHorizontal withFixedItemLength<span class="token punctuation">:</span><span class="token number">50</span> leadSpacing<span class="token punctuation">:</span><span class="token number">0</span> tailSpacing<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="优雅的限制输入框字符数量"><a href="#优雅的限制输入框字符数量" class="header-anchor">#</a> 优雅的限制输入框字符数量</h2> <p>有时候我们需要对文本框输入的字符数量进行限制，通常是在  <code>textFieldDidChange</code>  或  <code>textViewDidChange</code>  方法中利用字符串的  <code>substringToIndex</code>  方法截取指定长度。这种方法会在输入汉字时将候选时的拼音一起计入长度而出现实际输入字数过少的问题。这时我们可以通过  <code>markedTextRange</code>  来判断。emoji 表情在字符串中是以 2 个长度来处理的当遇到字符串截取时，如果截断位置刚好在 emoji 表情的中间，此时 emoji 表情就会出现无法解码。这时我们可以通过  <code>rangeOfComposedCharacterSequenceAtIndex</code>  方法来获取完整字符串的长度，该方法会将 emoji 表情视作一个连续的字符串，如果 index 处于连续的字符串之间，就会返回这个字符串的 range。</p> <p>下面的例子限制输入框最多输入 10 个字符</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>textFieldDidChange<span class="token punctuation">:</span><span class="token punctuation">(</span>UITextField <span class="token operator">*</span><span class="token punctuation">)</span>textField<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>textField<span class="token punctuation">.</span>text<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">[</span>textField markedTextRange<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 候选字符不计入长度判断</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 防止 emoji 被截断</span>
        NSRange range <span class="token operator">=</span> <span class="token punctuation">[</span>textField<span class="token punctuation">.</span>text rangeOfComposedCharacterSequenceAtIndex<span class="token punctuation">:</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        textField<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token punctuation">[</span>textField<span class="token punctuation">.</span>text substringToIndex<span class="token punctuation">:</span>range<span class="token punctuation">.</span>location<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="去掉视图的隐式动画"><a href="#去掉视图的隐式动画" class="header-anchor">#</a> 去掉视图的隐式动画</h2> <p>在刷新 tableView 的 section 时默认会有动画效果，如果我们想要去掉这个隐式动画可以使用
 <code>performWithoutAnimation</code></p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token punctuation">[</span>UIView performWithoutAnimation<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
     <span class="token comment">// 需要去掉动画的操作</span>
     <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section> <section aria-hidden="true" id="swift" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token builtin">UIView</span><span class="token punctuation">.</span>performWithoutAnimation <span class="token punctuation">{</span>
     <span class="token comment">// 需要去掉动画的操作</span>
     <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>      
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <p>上面这种方式无法取消  <code>CALayer</code>  的动画，要想取消  <code>CALayer</code>  的动画需要通过下面的方式</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objc line-numbers-mode"><pre class="language-text"><code>[CATransaction begin];
[CATransaction setDisableActions:YES];
// 需要去掉动画的操作
........  
[CATransaction commit];
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="swift" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token builtin">CATransaction</span><span class="token punctuation">.</span><span class="token function">setDisableActions</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
<span class="token comment">// 需要去掉动画的操作</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 
<span class="token builtin">CATransaction</span><span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <hr> <p>未完待续...</p></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/17.ee1bcf41.js" defer></script>
  </body>
</html>
